<template>
  <div class="updata-name">
    <!-- 导航栏 -->
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onConfim"
    />
    <!-- 导航栏 -->

    <!-- 输入框 -->
    <div class="field-wrap">
      <van-field
        v-model.trim="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
    <!-- 输入框 -->
  </div>
</template>

<script>
import { updataUserProfile } from "@/api/user";
export default {
  name: "UpdataName",
  data() {
    return {
      localName: this.value,
    };
  },
  props: {
    value: String,
    required: true,
  },
  methods: {
    async onConfim() {
      this.$toast.loading({
        message: "保存中...",
        forbidClick: true, //静止背景点击
        duration: 0,//持续展示
      });
      try {
        const localName = this.localName;
        if (!localName.length) {
          this.$toast("昵称不能为空");
          return;
        }
        const { data } = await updataUserProfile({
          name: localName,
        });
        // 更新视图
        this.$emit("input", localName);
        // 关闭弹层
        this.$emit("close");
        // 提示成功
        this.$toast.success("更新完成");
      } catch (err) {
        this.$toast("更新失败");
      }
    },
  },
};
</script>

<style scoped lang="less">
.field-wrap {
  padding: 10px;
}
</style>